<template>
  <t-image-viewer
    v-for="item in props.images"
    :key="item"
    :visible="current === item"
    :index="currentIndex"
    :images="props.images"
    @close="onClose"
    @index-change="onChange"
  >
    <template #trigger>
      <div class="tdesign-demo-image-viewer__ui-image tdesign-demo-image-viewer__base">
        <img alt="" :src="item" class="tdesign-demo-image-viewer__ui-image--img" />
        <div class="tdesign-demo-image-viewer__ui-image--hover">
          <t-space>
            <span @click="onPreview(item as string)"> <BrowseIcon size="1.4em" /> 预览 </span>
            <span @click="onDown(item as string)" v-if="!enableEdit"> <DownloadIcon size="1.4em" /> 下载 </span>
            <span @click="onEdit(item as string)" v-if="enableEdit"> <EditIcon size="1.4em" /> 编辑 </span>
          </t-space>
        </div>
      </div>
    </template>
  </t-image-viewer>
  <div class="result-container" v-if="props.images.length <= 0">
    <div class="result-bg-img">
      <component :is="Result403Icon"></component>
    </div>
    <div class="result-title">{{ props.title }}</div>
    <div class="result-tip">{{ tip }}</div>
    <slot />
  </div>

</template>


<script setup lang="ts">
import { ref, defineProps, defineEmits } from 'vue';
//import { MessagePlugin } from 'tdesign-vue-next';
import {downFile} from '@/utils/download'
import { BrowseIcon, DownloadIcon, EditIcon } from 'tdesign-icons-vue-next';

import Result403Icon from '@/assets/assets-result-403.svg?component';

const props = defineProps({
  title:{
    type:String,
    default:'暂无结果'
  },
  images: {
    type: Array,
    default:[]
  },
  enableEdit: {
    type: Boolean,
    default:false
  },
});
const emits = defineEmits(['onEdit']);
const tip = ref('');

const current = ref('');
const currentIndex = ref(0);
const onPreview = (imgUrl: string) => {
  current.value = imgUrl;
  currentIndex.value = props.images.findIndex((x) => x === imgUrl);
  console.log('imgUrl ' + imgUrl + ' currentIndex.value ' + currentIndex.value);
};
const onEdit = (url: string) => {
  //console.log('onEdit0',url)
  emits('onEdit', url);
};

const onDown = (url: string) => {
  console.log('url='+url)
  // 处理图片下载
  let newstring = url.substring(url.length - 4, url.length);
  if (['.png', '.jpg', 'jpeg', '.bmp', '.gif', 'webp', '.psd', '.svg', 'tiff'].indexOf(newstring) !== -1) {
    url = url + '?response-content-type=application/octet-stream';
  }
  downFile(url)
  // let a = document.createElement('a');
  // a.setAttribute('href', url);
  // a.setAttribute('download', '');
  // a.setAttribute('target', '_blank');
  // let clickEvent = document.createEvent('MouseEvents');
  // clickEvent.initEvent('click', true, true);
  // a.dispatchEvent(clickEvent);
};
const onClose = (url: string) => {
  current.value = '';
};
const onChange = (index: number) => {
  currentIndex.value = index;
};


current.value=props.images[0] as string
currentIndex.value=0
</script>
<style scoped>
  .result-container
  {
    text-align: center;
  }
.result-title {
}

.tdesign-demo-image-viewer__ui-image {
  width: 100%;
  height: 100%;
  display: inline-flex;
  position: relative;
  justify-content: center;
  align-items: center;
  border-radius: var(--td-radius-small);
  overflow: hidden;
}

.tdesign-demo-image-viewer__ui-image--hover {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: absolute;
  left: 0;
  top: 0;
  opacity: 0;
  background-color: rgba(0, 0, 0, 0.6);
  color: var(--td-text-color-anti);
  line-height: 22px;
  transition: 0.2s;
}

.tdesign-demo-image-viewer__ui-image:hover .tdesign-demo-image-viewer__ui-image--hover {
  opacity: 1;
  cursor: pointer;
}

.tdesign-demo-image-viewer__ui-image--img {
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  cursor: pointer;
  position: absolute;
}

.tdesign-demo-image-viewer__ui-image--footer {
  padding: 0 16px;
  height: 56px;
  width: 100%;
  line-height: 56px;
  font-size: 16px;
  position: absolute;
  bottom: 0;
  color: var(--td-text-color-anti);
  background-image: linear-gradient(0deg, rgba(0, 0, 0, 0.4) 0%, rgba(0, 0, 0, 0) 100%);
  display: flex;
  box-sizing: border-box;
}

.tdesign-demo-image-viewer__ui-image--title {
  flex: 1;
}

.tdesign-demo-popup__reference {
  margin-left: 16px;
}

.tdesign-demo-image-viewer__ui-image--icons .tdesign-demo-icon {
  cursor: pointer;
}

.tdesign-demo-image-viewer__base {
  width: 160px;
  height: 160px;
  margin: 10px;
  border: 4px solid var(--td-bg-color-secondarycontainer);
  border-radius: var(--td-radius-medium);
}
</style>
